%%
% 1.复指数序列
% x(n)=0.5^n * e^(j*0.8*n)  -2<=n<=10
n0=-2;
n2=10;
n=n0:n2;
x=(0.5.^n) .* exp(0.8*1i*n);
figure(1)
subplot(211)
stem(n,real(x),'.');
axis([-3,10,min(real(x))-0.5,max(real(x))+0.5])
title('复指数序列')
ylabel('实部');grid;
subplot(212)
stem(n,imag(x),'.');
axis([-3,10,min(imag(x))-0.5,max(imag(x))+0.5])
ylabel('虚部');xlabel('n');grid;

%%
% 2.已知序列x(n)=[1,2,3,4,5]，h(n)=[-5,4,7,9]
x=[1,2,3,4,5];n1=0:length(x)-1;
h=[-5,4,7,9];n2=0:length(h)-1;

% 绘制x(n),h(n)两个序列
subplot(221);stem(0:length(x)-1,x,'.');ylabel('x(n)');grid;

% 生成并绘制y_1(n)=x(n)+h(n)序列
% [y_1,n]=seqadd(x,n1,h,n2);
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x;
y2((n>=min(n2))&(n<=max(n2))==1)=h;
y_1=y1+y2;
subplot(222);stem(n,y_1,'.');ylabel('y_1(n)=x(n)+h(n)');grid;

% 生成并绘制y_2(n)=x(n)h(n)序列
% [y_2,n]=convwthn(x,n1,h,n2);
y_2=conv(x,h);
ny1=n1(1)+n2(1);
ny2=n1(length(x))+n2(length(h));
n=ny1:ny2;
subplot(223);stem(n,y_2,'.');ylabel('y_2(n)=x(n)h(n)');grid;

% 生成并绘制y_3(n)=x(n)*h(n)序列
% [y_3,n]=seqmult(x,n1,h,n2);
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));y2=y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x;
y2((n>=min(n2))&(n<=max(n2))==1)=h;
y_3=y1.*y2;
subplot(224);stem(n,y_3,'.');ylabel('y_3(n)=x(n)*h(n)');grid;

%%
% 3.已知序列x(n)=[1,2,3,4,5,6,7,8,9]
x=[1,2,3,4,5,6,7,8,9];n=0:length(x)-1;

% 绘制x(n)序列
figure(1);stem(n,x,'.');ylabel('x(n)');grid;


% 生成并绘制将该序列延拓3个周期后得到的序列
k=3;
nx=0:length(x)-1;
ny=0:(k*length(x)-1);
y=x(mod(ny,length(x))+1);
figure(2);subplot(221);stem(ny,y,'.');ylabel('y(n)');grid;

% 生成并绘制y_1(n)=x(n-3)序列
% [y_1,ny_1]=seqshift(x,n,-3);
ny_1=n+3;
y_1=x;
subplot(222);stem(ny_1,y_1,'.');ylabel('y_1(n)=x(n-3)');grid;

% 生成并绘制y_2(n)=x(n+2)序列
% [y_2,ny_2]=seqshift(x,n,2);
ny_2=n-2;
y_2=x;
subplot(223);stem(ny_2,y_2,'.');ylabel('y_2(n)=x(n+2)');grid;

% 生成并绘制y_3(n)=x(-n)序列
% [y_3,ny_3]=seqfold(x,n);
y_3=fliplr(x);
ny_3=-fliplr(n);
subplot(224);stem(ny_3,y_3,'.');ylabel('y_3(n)=x(-n)');grid;


%%
% 4.已知序列x(n)=[5,4,3,2,1,1,2,3,4,5],求它的DTFT，并画出幅频和相频特性图
x=[5,4,3,2,1,1,2,3,4,5];n=0:length(x)-1;
% 方法一
k=0:1000;w=(pi/500)*k;
X=x*(exp(-1j*pi/500)).^(n'*k);
% 方法二
% a=1;N=1000;
% [X,w]=freqz(x,a,N,'whole');
magX=abs(X);angX=angle(X);
subplot(121);plot(w/pi,magX);grid;
xlabel('w/pi');ylabel('模值');title('幅频特性');
subplot(122);plot(w/pi,angX);grid;
xlabel('w/pi');ylabel('弧度');title('相频特性');



